import os
from openpyxl import Workbook,load_workbook

from cases import titles,cases_id,limits_H,limits_L,methods
from commons.common import getLastDate



class ReportXlsx:
    file_xlsx_path = f'./Reports/BL133_report_{getLastDate()}.xlsx'
    file_xlsx_dir = './Reports'

    @staticmethod
    def init():
        # global titles,cases_id,limits_H,limits_L,methods
        workbook = Workbook()
        # 默认sheet
        sheet = workbook.active
        sheet.title = "sheet1"
        flag = True
        if not os.path.exists(ReportXlsx.file_xlsx_dir):
            os.mkdir(ReportXlsx.file_xlsx_dir)
            flag = False
        if not os.path.exists(ReportXlsx.file_xlsx_path):
            # Logger.logger.info('正在初始化并创建excel文件。。。')
            # titles, cases_id, limits_H, methods, limits_L
            empolyee_titles = ['','','','','测试项标题']
            empolyee_ids = ['', '', '', '', '测试项编号']
            empolyee_limits_H = ['', '', '', '', '上限']
            empolyee_methods = ['', '', '', '', '匹配方法']
            empolyee_limits_L = ['', '', '', '', '下限']

            empolyee_titles.extend(titles)
            empolyee_ids.extend(cases_id)
            empolyee_limits_H.extend(limits_H)
            empolyee_methods.extend(methods)
            empolyee_limits_L.extend(limits_L)
            print(empolyee_methods)

            empolyee = ['SN', '日期时间', '测试时间', '测试结果','FAIL项']
            empolyee.extend(titles)

            length = len(empolyee_methods)

            # sheet.cell(4,1).data_type = "s"

            sheet.append(empolyee_titles)
            sheet.append(empolyee_ids)
            sheet.append(empolyee_limits_H)
            sheet.append(empolyee_methods)
            sheet.append(empolyee_limits_L)
            sheet.append(empolyee)
            workbook.save(ReportXlsx.file_xlsx_path)  # 创建excel文件
            # Logger.logger.info('初始化创建excel文件——成功')
            flag = False


    # 追加测试记录————写死了
    @staticmethod
    def report_write_data(l:list|tuple):
        # Logger.logger.info('正在记录测试数据')
        wb = load_workbook(ReportXlsx.file_xlsx_path)
        sheet = wb.active
        empolyee=tuple(l)
        sheet.append(empolyee)
        wb.save(ReportXlsx.file_xlsx_path)
        # Logger.logger.info('记录测试数据——成功')

# ReportXlsx.report_write_data([i for i in range(12)])
